package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.PaymentContract;
import ywpt.model.ProviderInfo;

import ywpt.service.config.ProviderService;
import ywpt.service.contract.PaymentContractService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ProviderAction extends BaseAction implements
		ModelDriven<ProviderInfo>, Preparable {



	/**
	 * 供应商Action
	 */
	private static final long serialVersionUID = 1990262055019134293L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	
	private ProviderInfo providerInfo;
	private ProviderService providerService;
	private List<ProviderInfo> providerInfoList = new ArrayList<ProviderInfo>();
	
	private PaymentContractService paymentContractService;
	//查询相关
	private String qCode;
	private String qName;
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public ProviderInfo getProviderInfo() {
		return providerInfo;
	}
	public void setProviderInfo(ProviderInfo providerInfo) {
		this.providerInfo = providerInfo;
	}
	public ProviderService getProviderService() {
		return providerService;
	}
	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}
	public List<ProviderInfo> getProviderInfoList() {
		return providerInfoList;
	}
	public void setProviderInfoList(List<ProviderInfo> providerInfoList) {
		this.providerInfoList = providerInfoList;
	}
	
	public String getQCode() {
		return qCode;
	}
	public void setQCode(String code) {
		qCode = code;
	}
	public String getQName() {
		return qName;
	}
	public void setQName(String name) {
		qName = name;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public PaymentContractService getPaymentContractService() {
		return paymentContractService;
	}
	public void setPaymentContractService(
			PaymentContractService paymentContractService) {
		this.paymentContractService = paymentContractService;
	}
	// ****************************默认方法*******************************
	@Override
	public ProviderInfo getModel() {
		return this.providerInfo;
	}
 
	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.providerInfo = providerService.getProviderInfoById(id);
		} else {
			this.providerInfo = new ProviderInfo();
		}
	}
	
	protected void getDateList(DetachedCriteria query) {
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = providerService.getProviderInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("code"));
		query.setProjection(null);
		providerInfoList.addAll(providerService.getProviderInfos(query, pageSize,rowStartEnd[0]));		
	}

	// **************************数据交互方法*******************************

	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(ProviderInfo.class);
		getDateList(query);
		return SUCCESS;
	}

	public String input() 
	{
		String code =this.providerService.createProviderCode();
		providerInfo.setCode(code);
		return INPUT;
	}
	
	public String edit() 
	{
		this.providerInfo=this.providerService.getProviderInfoById(id);
		return INPUT;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		System.out.println("方问Action成功1");
		try 
		{
			if (id == null) 
			{
				providerService.saveProviderInfo(providerInfo);
			} 
			else 
			{
				providerService.updateProviderInfo(providerInfo);
			}
			this.addActionMessage("信息已保存!");
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
			System.out.println("方问Action成功2");
			if (e.getMessage().indexOf("already exists") != -1) 
			{
				this.addFieldError("clientCode", "编码重复");
			} 
			else 
			{
				this.addActionError("保存错误:" + e.getMessage());
			}
			return INPUT;
		}
	}
	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			DetachedCriteria paymentQuery = DetachedCriteria.forClass(PaymentContract.class);
			paymentQuery.add(Restrictions.eq("provider", providerService.getProviderInfoById(id)));
			int	totalRows1 = paymentContractService.getPaymentContractsCountByCriteria(paymentQuery);
			if(totalRows1>0)
			{
				this.addActionMessage("该费用类别已经发生过业务逻辑，不能删除！");
				return INPUT;
			}else{
				providerService.removeProviderInfo(id);
				return SUCCESS;				
			}

		}
		this.addActionMessage("客户编号为空，请选择具体的记录！");
		return INPUT;
	}
	
	public String search() throws Exception 
	{
		if (!StringTools.isNullOrSpace(this.qCode)||!StringTools.isNullOrSpace(this.qName)) {
			DetachedCriteria query = DetachedCriteria.forClass(ProviderInfo.class);
			if (!StringTools.isNullOrSpace(this.qCode)) {
				query.add(Restrictions.like("code", "%"+this.qCode+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qName)) {
				query.add(Restrictions.like("name", "%"+this.qName+"%"));
			}
			getDateList(query);
			return SUCCESS;			
		}else{
			this.addActionMessage("请输入查询内容！");
			return INPUT;
		}

	}
	
	
	
	public String select() 
	{
		providerInfoList.addAll(providerService.getProviderInfos());
		return SUCCESS;
	}




}
